//
//  QSBaseViewController.m
//  testDemo
//
//  Created by apple on 2017/6/20.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "QSBaseViewController.h"

@interface QSBaseViewController ()
/** 状态栏样式 */
@property (nonatomic, assign) UIStatusBarStyle statusBarStyle;
/** 状态栏是否隐藏 */
@property (nonatomic, assign) BOOL statusBarHidden;
@end

@implementation QSBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 不穿透导航栏
    [self setViewRectEdge:UIRectEdgeNone];
    
    //改变状态栏
    [self changeStatusBarStyle:UIStatusBarStyleLightContent statusBarHidden:NO changeStatusBarAnimated:YES];
    
    //设置背景色
    self.view.backgroundColor = kWhite_color;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.view endEditing:YES];
    
    [[QSNetworkTool shareNetworkInstance] cancelAllOperations];
}

#pragma mark - System
/**
 *  修改状态栏样式
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    if (self.statusBarStyle) {
        return self.statusBarStyle;
    }else{
        return UIStatusBarStyleDefault;
    }
}

- (BOOL)prefersStatusBarHidden
{
    return self.statusBarHidden;
}
/************************************************/

#pragma mark - set
/**
 *  是否穿透导航栏 UIRectEdgeNone：不穿透； UIRectEdgeAll：穿透
 */
- (void)setViewRectEdge:(UIRectEdge)viewRectEdge
{
    _viewRectEdge = viewRectEdge;
    
    UINavigationController *nav = self.navigationController;
    if (viewRectEdge == UIRectEdgeAll) {
        nav.navigationBar.translucent = YES;
        self.automaticallyAdjustsScrollViewInsets = NO;
    } else {
        nav.navigationBar.translucent = NO;
        self.automaticallyAdjustsScrollViewInsets = YES;
    }
    self.edgesForExtendedLayout = viewRectEdge;
}

/**
 *  修改StatusBar
 */
- (void)changeStatusBarStyle:(UIStatusBarStyle)statusBarStyle statusBarHidden:(BOOL)statusBarHidden changeStatusBarAnimated:(BOOL)animated
{
    self.statusBarStyle = statusBarStyle;
    self.statusBarHidden = statusBarHidden;
    if (animated) {
        [UIView animateWithDuration:0.25 animations:^{
            [self setNeedsStatusBarAppearanceUpdate];
        }];
    }else{
        [self setNeedsStatusBarAppearanceUpdate];
    }
}

/**
 *  隐藏显示导航栏
 */
- (void)hideNavigationBar:(BOOL)isHide animated:(BOOL)animated
{
    if (animated) {
        [UIView animateWithDuration:0.25 animations:^{
            
            [self.navigationController setNavigationBarHidden:isHide];
        }];
    }else{
        [self.navigationController setNavigationBarHidden:isHide];
    }
}
@end
